package studentsystem;

import java.util.Scanner;
import java.sql.*;

public class Input {
    private String b, c;
    private double d, e, f, g;
    private double sum, aver;
    Scanner in = new Scanner(System.in);
    private int a = 2;

    Input() {
        while (a != 0) {
            System.out.println("---------------------");
            System.out.println("1 录入成绩");
            System.out.println("0 返回");
            a = in.nextInt();
            switch (a) {
                case 0:
                    System.out.println("返回成功！");
                    break;
                case 1:
                    System.out.println("请输入学生姓名：");
                    b = in.next();
                    System.out.println("请输入学生学号：");
                    c = in.next();
                    System.out.println("请输入学生应用数学成绩：");
                    d = in.nextDouble();
                    System.out.println("请输入学生大学英语成绩：");
                    e = in.nextDouble();
                    System.out.println("请输入学生Java程序设计成绩：");
                    f = in.nextDouble();
                    System.out.println("请输入学生计算机应用基础成绩：");
                    g = in.nextDouble();
                    sum = d + e + f + g;   //总成绩
                    aver = sum / 4;    //平均成绩
                    //数据库
                    SQL bd = new SQL();
                    Connection conn = bd.getConn();
                    try {
                        Statement stmt = conn.createStatement(); //创建Statement对象
                        try {
                            String sql = "insert into students values(?,?,?,?,?,?,?,?)"; //要执行的SQL
                            PreparedStatement pst = conn.prepareStatement(sql);
                            pst.setString(1, b);                             //传入带占位符的SQL语句
                            pst.setString(2, c);
                            pst.setDouble(3, d);
                            pst.setDouble(4, e);
                            pst.setDouble(5, f);
                            pst.setDouble(6, g);
                            pst.setDouble(7, sum);
                            pst.setDouble(8, aver);
                            pst.executeUpdate(); //执行 update和insert、delete等sql语句
                            System.out.println("保存成功！");
                            pst.close();
                        } catch (SQLIntegrityConstraintViolationException e) {
                            System.out.println("该学生已存在！");
                        }
                        stmt.close();
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    //数据库
                    break;
                default:
                    System.out.println("输入错误！");
            }
        }
    }

    public static void main(String[] args) {
        new Input();
    }
}